package com.liu.service.impl;

import com.liu.entity.Stage;
import com.liu.mapper.StageMapper;
import com.liu.service.StageService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Stage)表服务实现类
 *
 * @author makejava
 * @since 2023-02-10 10:43:41
 */
@Service("stageServiceImpl")
@Transactional
public class StageServiceImpl implements StageService {
    @Resource
    private RedisTemplate<String, Integer> redisTemplate;

    @Resource
    private StageMapper stageMapper;

    @Override
    public Stage queryById(Integer id) {
        return this.stageMapper.queryById(id);
    }

    @Override
    public List<Stage> findAll() {
        return this.stageMapper.findAll();
    }

    @Override
    public List<Stage> findStageByPhy(Integer id) {
        return this.stageMapper.findStageByPhy(id);
    }

    @Override
    public List<Stage> findStage(Integer pageNum,Integer pageSize,Integer id) {
        return this.stageMapper.findStage(pageNum, pageSize, id);
    }

    @Override
    public Long StageCount(Integer id) {
        return this.stageMapper.StageCount(id);
    }

    @Override
    public List<Stage> queryAllByLimit(Integer pageNum, Integer pageSize) {
        return this.stageMapper.queryAllByLimit(pageNum,pageSize);
    }

    @Override
    public long count() {
        return this.stageMapper.count();
    }

//    @Override
//    public int insert(Stage stage) {
//        return this.stageMapper.insert(stage);
//    }
    // 新增阶段
    @Override
    public int insert(Stage stage) {
        // 1. 初始化 count（若未设置，默认为 0）
        if (stage.getCount() == null) {
            stage.setCount(0);
        }
        // 2. 初始化 version（乐观锁版本号，默认为 0）
        if (stage.getVersion() == null) {
            stage.setVersion(0);
        }
        // 3. 插入数据库
        int insertRows = stageMapper.insert(stage);
        if (insertRows > 0) {
            // 4. 插入成功后，同步剩余名额到 Redis
            String redisKey = "stage:remaining:" + stage.getId();
            // 剩余名额 = 最大名额（num） - 已用名额（count）
            int remaining = stage.getNum() - stage.getCount();
            redisTemplate.opsForValue().set(redisKey, remaining);
        }
        return insertRows;
    }

    @Override
    public int insertBatch(List<Stage> entities) {
        return this.stageMapper.insertBatch(entities);
    }

    @Override
    public int update(Stage stage) {
        int updateRows = this.stageMapper.update(stage);
        // 若更新了 num 或 count，需重新计算剩余名额并同步到 Redis
        if (updateRows > 0) {
            String redisKey = "stage:remaining:" + stage.getId();
            int remaining = stage.getNum() - stage.getCount();
            redisTemplate.opsForValue().set(redisKey, remaining);
        }
        return updateRows;
    }

    @Override
    public int updateCou(Integer count, Integer id) {
        return this.stageMapper.updateCou(count,id);
    }

    @Override
    public int deleteById(Integer id) {
        return this.stageMapper.deleteById(id);
    }

    @Override
    public int deleteByIds(List<Integer> ids) {
        return this.stageMapper.deleteByIds(ids);
    }

    @Override
    public Stage queryByIdWithVersion(int staId) {
        return this.stageMapper.queryByIdWithVersion(staId);
    }

    @Override
    public int updateCountWithOptimisticLock(int staId, Integer version) {
        return this.stageMapper.updateCountWithOptimisticLock(staId, version);
    }
}
